package com.tjakus.mail;

import com.tjakus.hibernate.Users;
import com.tjakus.http.HttpTrafficManager;
import com.tjakus.logging.Logging;
import com.tjakus.properties.Property;

/**
 * This class sends e-mail over net.hr account
 * 
 * @author tjakus
 * 
 */

public class MailOverHttp {

	private String httpGetUrl;
	private String httpPostUrl;
	private String postContent;
	private String cookies;
	private boolean status;

	/**
	 * This method is responsible to send e-mail over net.hr account. It returns
	 * boolean status if mail has been successfully sent or not
	 * 
	 */
	public boolean postMail(Users user, String subject, String message) {
		Logging.getLogger().info("Mail - postMail - []");
		if (user != null) {
			// HTTP POST to send e-mail
			httpPostUrl = "http://freemail.net.hr/?m=newmsg&op=send&w=draft";
			// Create POST body with user name and password
			postContent = "smart_input_to=&smart_input_cc=&cc=&smart_input_bcc=&bcc=&subject="
					+ subject
					+ "&attach[]=&draft=0&html=1&priority=3&write=new&backupFolderId=1&msg_id=&"
					+ "body="
					+ message
					+ "&htmledit=1&att=&alias=mali.oglas%40net.hr&orig_msg_id=&orig_write=&template=0&type=email&to=%3C"
					+ user.getEmail().replace("@", "%40")
					+ "%3E&x_body=%3Cp%3E%3C%2Fp%3E%3Cp%3E%3C%2Fp%3E";
			status = HttpTrafficManager.sendHttpPostWithStringContent(httpPostUrl,
					cookies, postContent);
			if (status) {
				Logging.getLogger().info(
						"Mail - postMail - e-mail sent [ " + user.getEmail() + " ]");
			} else {
				Logging.getLogger().error(
						"Mail - postMail - e-mail has NOT been sent [ " + user.getEmail()
								+ " ]");
			}
			return status;
		} else {
			Logging.getLogger().info(
					"Mail - postMail - [ e-mail has NOT been sent, no recipients ]");
			return false;
		}
	}

	/**
	 * This method is responsible to log in to net.hr account with cookies.
	 * 
	 */
	public boolean logInToEmailAccount() {
		// HTTP GET to get cookie
		Logging.getLogger().info("postMail - logInToEmailAccount [ LOG IN ]");
		httpGetUrl = "http://ad.net.hr/js.ng/site=net.hr&adsize=300x250&PagePos=1&channel=gigamail&ran=1354825961424&tile=39750733";
		cookies = HttpTrafficManager.sendHttpGetToGetCookies(httpGetUrl);
		// If cookies are unable to retrieve, exit from method
		if (cookies == null) {
			Logging.getLogger().error(
					"postMail - logInToEmailAccount [ cookies are null ]");
			return false;
		}
		// HTTP POST request for log in with cookies
		httpPostUrl = "http://user.net.hr/?url=http%3A%2F%2Ffreemail.net.hr%2F%3F";
		// Create POST body with user name and password
		postContent = "ego_domain=net.hr&url=http%3A%2F%2Ffreemail.net.hr%2F&ego_user="
				+ Property.EMAIL_USERNAME + "&ego_secret=" + Property.EMAIL_PASSWORD;
		cookies = HttpTrafficManager.sendHttpPostWithStringContentToGetCookies(
				httpPostUrl, cookies, postContent);
		// If cookies are unable to retrieve, exit from method
		if (cookies == null) {
			return false;
		}
		return true;
	}

	/**
	 * This method is responsible to log out from net.hr account with cookies.
	 * 
	 */
	public void logOutFromEmailAccount() {
		Logging.getLogger().info("postMail - logOutFromEmailAccount [ LOG OUT ]");
		// HTTP get to log out
		httpGetUrl = "http://freemail.net.hr/?ego_logout=1&url=%2Ffree%2Flogout.php%3Flogout%3D5%26name%3Dmali.oglas%40net.hr";
		HttpTrafficManager.sendHttpGet(httpGetUrl, cookies);
	}

}
